package com.home.qh.dao.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.home.qh.dao.CustomerDao;
import com.home.qh.model.Address;
import com.home.qh.model.City;
import com.home.qh.model.Customer;

@Repository("customerDao")
public class CustomerDaoHibernate extends GenericDaoHibernate<Customer, Long> implements
		CustomerDao {

	public CustomerDaoHibernate() {
		super(Customer.class);
	}
	
	public List<Customer> findCustomer(String textSearch, Date startDate, Date endDate,
			List<Address> addresses, String sortColumn, boolean sortType, int startResult,
			int maxResults) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
        if(textSearch != null && !"".equals(textSearch)) {
            criteria.add(
                    Restrictions.disjunction().add(Restrictions.ilike("fullName", textSearch, MatchMode.ANYWHERE))
                    .add(Restrictions.ilike("description", textSearch, MatchMode.ANYWHERE)));
        }
        
        if(startDate != null && !"".equals(startDate) && endDate != null && !"".equals(endDate)) {
            criteria.add(Restrictions.between("joinDate",startDate,endDate));
        } else if(startDate != null && !"".equals(startDate)) {
            criteria.add(Restrictions.ge("joinDate",startDate));
        } else if(endDate != null && !"".equals(endDate)) {
            criteria.add(Restrictions.le("joinDate",endDate));
        }

        if(addresses != null && addresses.size() > 0) {
//            DetachedCriteria currentStatus = DetachedCriteria.forClass(Address.class, "add")
//            .setProjection( Property.forName("id") )
//            .add( Property.forName("cusali.customer").eqProperty("aliases.customer") );
//
//            criteria.createCriteria("aliases", "aliases").add(Property.forName("id").in(currentStatus));
        }

        if(sortColumn != null && !"".equalsIgnoreCase(sortColumn)) {
            if(sortType) {
                criteria.addOrder(Order.desc(sortColumn));
            }else criteria.addOrder(Order.asc(sortColumn));
        }

        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        
        return getHibernateTemplate().findByCriteria(criteria, startResult, maxResults);
	}

	public List<Customer> getCustomerByName(String customerName) {
		List<Customer> customers = getHibernateTemplate().find("from Customer where fullName like '%?%'", customerName);
        if (customers.isEmpty()) {
            return null;
        } else {
            return customers;
        } 
	}

	public List<Customer> getCustomerByCustomer(Customer customer, String sort,
			boolean desc, int startResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);

        if (customer.getFullName() != null) {
        	criteria.add(Restrictions.like("fullName", customer.getFullName(),MatchMode.ANYWHERE));
		}
        
        if (customer.getContact() != null) {
        	if(customer.getContact().getName()!=null && !"".equals(customer.getContact().getName())){
        		criteria.add(Restrictions.like("contact.name", customer.getContact().getName(), MatchMode.ANYWHERE));
        	}
        	if(customer.getContact().getPhone()!=null && !"".equals(customer.getContact().getPhone())){
        		criteria.add(Restrictions.like("contact.phone", customer.getContact().getPhone(), MatchMode.ANYWHERE));
        	}
        	if(customer.getContact().getFax()!=null && !"".equals(customer.getContact().getFax())){
        		criteria.add(Restrictions.like("contact.fax", customer.getContact().getFax(), MatchMode.ANYWHERE));
        	}
        	if(customer.getContact().getWebsite()!=null && !"".equals(customer.getContact().getWebsite())){
        		criteria.add(Restrictions.like("contact.website", customer.getContact().getWebsite(), MatchMode.ANYWHERE));
        	}
        	if(customer.getContact().getEmail()!=null && !"".equals(customer.getContact().getEmail())){
        		criteria.add(Restrictions.like("contact.email", customer.getContact().getEmail(), MatchMode.ANYWHERE));
        	}
		}
        
        if (customer.getBalance()>0D){
        	criteria.add(Restrictions.eq("balance", customer.getBalance()));
        }

        if(sort != null && !"".equalsIgnoreCase(sort)) {
            if(desc) criteria.addOrder(Order.desc(sort));
            else criteria.addOrder(Order.asc(sort));
        }

        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        
        return getHibernateTemplate().findByCriteria(criteria, startResult, maxResults);
	}

}
